package com.baizhi.wwk.service;

import com.baizhi.wwk.dao.CategoryMapper;
import com.baizhi.wwk.entity.Category;
import com.baizhi.wwk.entity.CategoryExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("categoryService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Map<String, Object> queryOnePage(int page, int pageSize) {
        Map<String,Object> map=new HashMap<>();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdIsNull();

        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int count = categoryMapper.selectCount(null);
        map.put("page",page);
        map.put("total",count);
        map.put("rows",categoryList);
        return map;
    }

    @Override
    public void addFirst(Category category) {

        if (category.getParentId()==null){
            category.setLevels(1);
        }else {
            category.setLevels(2);
        }
        categoryMapper.insertSelective(category);

    }

    @Override
    public Map<String, Object> delete(int id) {
        Map<String,Object> map=new HashMap<>();

        categoryMapper.deleteByPrimaryKey(id);
        map.put("message","操作成功");
        return map;
    }

    @Override
    public List<Category> queryByLevelsCategory() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdIsNull();
        List<Category> categoryList=categoryMapper.selectByExample(categoryExample);

        return categoryList;
    }

    @Override
    public void update(Category category) {
        System.out.println(category);
        Category category1 = new Category();
        if (category.getParentId()==null){
            category1.setLevels(1);
        }else {
            category1.setLevels(2);
        }
        category1.setId(category.getId());
        category1.setCateName(category.getCateName());
        System.out.println(category1);
        categoryMapper.updateByPrimaryKey(category1);

    }

    @Override
    public Category queryById(int id) {
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(id);
        Category category = categoryMapper.selectOneByExample(categoryExample);
        return category;
    }

    @Override
    public Map<String, Object> queryTwoPage(int page, int pageSize, int categoryId) {
        Map<String,Object> map=new HashMap<>();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(categoryId);
        RowBounds rowBounds = new RowBounds((page-1)*pageSize, pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int count = categoryMapper.selectCount(null);
        map.put("page",page);
        map.put("total",count);
        map.put("rows",categoryList);
        return map;
    }
}
